//
//  YHMachineServiceChargeItemCell.m
//  YHApp
//
//  Created by -- on 2021/7/24.
//

#import "YHMachineServiceChargeItemCell.h"
#import "HAMachineServiceChargeMo.h"
#import "PORegexTool.h"

@interface YHMachineServiceChargeItemCell ()<UITextFieldDelegate>

/***/
@property (nonatomic, strong) UIView *mainV;
/***/
@property (nonatomic, strong) UIView *lineView;
/***/
@property (nonatomic, strong) UILabel *exLabel;
/***/
@property (nonatomic, strong) UIButton *addButton;
/***/
@property (nonatomic, strong) UIButton *minusButton;
/***/
@property (nonatomic, strong) UITextField *numFextField;
/***/
@property (nonatomic, strong) UILabel *titLabel;

@end

@implementation YHMachineServiceChargeItemCell

-(void)addButtonClick {
    
    float newValue = [_rateModel.value doubleValue]*1000;
    float max_money = [_rateModel.max_money doubleValue]*1000;

    newValue += 1000;
    
    if (newValue > max_money) {
        [MBProgressHUD showError:@"超过最大值"];
    }else {
        //设置新值
        float valueFloat = (float)(newValue*0.001);
        NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
        newValueString = [NSString changeFloat:newValueString];
        _rateModel.value = newValueString;
        self.numFextField.text = _rateModel.value;
    }
}

-(void)minusButtonClick {
    float newValue = [_rateModel.value floatValue]*1000;
    float min_money = [_rateModel.min_money floatValue]*1000;

    newValue -= 1000;
    
    if (newValue <  min_money) {
        [MBProgressHUD showError:@"小于最小值"];
    }else {
        //设置新值
        float valueFloat = (float)(newValue*0.001);
        NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
        newValueString = [NSString changeFloat:newValueString];
        _rateModel.value = newValueString;
        self.numFextField.text = _rateModel.value;
    }
}


-(void)textFieldDidEndEditing:(UITextField *)textField {
    float max_money = [_rateModel.max_money doubleValue]*1000;
    float min_money = [_rateModel.min_money floatValue]*1000;
    float newValue = [textField.text floatValue]*1000;
    if (newValue <  min_money) {
        [MBProgressHUD showError:@"小于最小值"];
        textField.text = _rateModel.value;
        return;
    }
    if (newValue > max_money) {
        [MBProgressHUD showError:@"超过最大值"];
        textField.text = _rateModel.value;
        return;
    }
    
    float valueFloat = (float)(newValue*0.001);
    NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
    newValueString = [NSString changeFloat:newValueString];
    _rateModel.value = newValueString;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.numFextField) {
        
        NSString *textStr = textField.text;
        NSString *t = [NSString stringWithFormat:@"%@%@",textStr,string];
        //所有长度大于6，无视输入
        //        if (t.length > 7) {
        //            return NO;
        //        }
        //限制输入小数点和数字
        BOOL isT1 = [PORegexTool checkInputOnlyPointNumber:t];
        //检测第一个输入的是否是小数点
        if (t.length > 0) {
            BOOL isPoint = [PORegexTool checkInputFristPoint:[t substringToIndex:1]];
            if (isPoint) {
                self.numFextField.text = @"0.";
                return NO;
            }
        }
        //已经包含小数点时，无视输入小数点
        if([textStr rangeOfString:@"."].location != NSNotFound) {
            if([string isEqualToString:@"."]){
                return NO;
            }
        }
        //检测小数点后面是否满2位
        BOOL isT2 = [PORegexTool checkAfterPointLenth:t];
        //检测
        if (!isT2 && isT1) {
            return YES;
        }else{
            return NO;
        }
    }
    
    return YES;
}

-(void)setRateModel:(YHMachineServiceChargeRatesModel *)rateModel {
    _rateModel = rateModel;
    
    if (self.isNotEdit) {
        self.addButton.hidden = YES;
        self.minusButton.hidden = YES;
        self.numFextField.enabled = NO;
    }else {
        self.addButton.hidden = NO;
        self.minusButton.hidden = NO;
        self.numFextField.enabled = YES;
    }
    self.titLabel.text = _rateModel.name;
    self.numFextField.text = _rateModel.value;

    self.exLabel.text = [NSString stringWithFormat:@"设置范围%@~%@",_rateModel.min_money,_rateModel.max_money];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.contentView.backgroundColor = [UIColor khaBGColor];
    
    [self.mainV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-10);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_offset(0.8);
        make.top.mas_equalTo(54);
    }];
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(13);
        make.width.mas_offset(28);
        make.height.mas_offset(28);
    }];
    
    [self.numFextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.addButton.mas_left).offset(-4);
        make.centerY.mas_equalTo(self.addButton);
        make.width.mas_offset(80);
        make.height.mas_offset(28);
    }];
    
    [self.minusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.numFextField.mas_left).offset(-4);
        make.centerY.mas_equalTo(self.addButton);
        make.width.mas_offset(28);
        make.height.mas_offset(28);
    }];

    [self.titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.addButton);
        make.right.mas_equalTo(self.minusButton.mas_left).offset(-10);
    }];

    [self.exLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(7);
        make.right.mas_equalTo(-20);
    }];
}

- (UIView *)mainV
{
    if (!_mainV) {
        _mainV = [UIView ha_createView];
        _mainV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_mainV];
    }
    return _mainV;
}

- (UIButton *)addButton
{
    if (!_addButton) {
        _addButton = [[UIButton alloc]init];
        [_addButton setTitle:@"+" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor colorWithHexString:@"#858B9C"] forState:UIControlStateNormal];
        _addButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _addButton.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
        _addButton.layer.cornerRadius = 4;
        _addButton.layer.masksToBounds = YES;
        [_addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.mainV addSubview:_addButton];
    }
    return _addButton;
}

- (UIButton *)minusButton
{
    if (!_minusButton) {
        _minusButton = [[UIButton alloc]init];
        [_minusButton setTitle:@"-" forState:UIControlStateNormal];
        [_minusButton setTitleColor:[UIColor colorWithHexString:@"#858B9C"] forState:UIControlStateNormal];
        _minusButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _minusButton.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
        _minusButton.layer.cornerRadius = 4;
        _minusButton.layer.masksToBounds = YES;
        [_minusButton addTarget:self action:@selector(minusButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.mainV addSubview:_minusButton];
    }
    return _minusButton;
}

-(UITextField *)numFextField
{
    if(!_numFextField)
    {
        _numFextField = [[UITextField alloc] init];
        _numFextField.textAlignment = NSTextAlignmentCenter;
        _numFextField.font = [UIFont systemFontOfSize:14];
        _numFextField.keyboardType = UIKeyboardTypeDecimalPad;
        _numFextField.delegate = self;
        _numFextField.placeholder = @"";
        _numFextField.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
        _numFextField.layer.cornerRadius = 4;
        _numFextField.layer.masksToBounds = YES;
        _numFextField.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
//        _numFextField.layer.borderColor = [UIColor colorWithHexString:@"#858B9C"].CGColor;
//        _numFextField.layer.borderWidth = 0.5;
//        _numFextField.layer.cornerRadius = 0;
//        _numFextField.layer.masksToBounds = YES;
//        [_numFextField addTarget:self action:@selector(textFieldEditChanged:)forControlEvents:UIControlEventEditingChanged];
        [self.mainV addSubview:_numFextField];
    }
    return _numFextField;
}


- (UILabel *)titLabel
{
    if (!_titLabel) {
        _titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#000000" text:@"" alignment:0];
        [self.mainV addSubview:_titLabel];
    }
    return _titLabel;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [UIView ha_createView];
        _lineView.backgroundColor = [UIColor kLineColor];
        [self.mainV addSubview:_lineView];
    }
    return _lineView;
}

- (UILabel *)exLabel
{
    if (!_exLabel) {
        _exLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#858b9c" text:@"" alignment:0];
        [self.mainV addSubview:_exLabel];
    }
    return _exLabel;
}

@end
